import $service from '../../service.js'
const app = getApp()
let timer = null;
Component({
  properties: {
    feeData: {
      type: Object,
      value: {},
      observer(newValue, oldValue) {
        if (!newValue) return
        this.timeDown()
      }
    },
    isFee: {
      type: Boolean,
      value: false
    }
  },
  data: {
    feeData: {
      feeData: ''
    },
    timeDown: {
       hh: '00',
       mm: '00',
       ss: '00'
    }
  },
  lifetimes: {
    detached() {
      clearTimeout(timer)
    },
  },
  methods: {
    timeDown() {
      let startTime = (Date.parse( new Date() ).toString()).substr(0, 10)
      let endTime;
      if (this.data.feeData ?. payEndTime) {
        endTime = ($service.timeFormat(this.data.feeData.payEndTime).toString()).substr(0, 10);
      }
      if(!endTime) return;
          var ts = endTime - startTime;//计算剩余的毫秒数
          var dd = parseInt(ts  / 60 / 60 / 24, 10);//计算剩余的天数
          var hh = parseInt(ts  / 60 / 60 % 24, 10);//计算剩余的小时数
          var mm = parseInt(ts  / 60 % 60, 10);//计算剩余的分钟数
          var ss = parseInt(ts  % 60, 10);//计算剩余的秒数
          dd = this.checkTime(dd);
          hh = this.checkTime(hh);
          mm = this.checkTime(mm);
          ss = this.checkTime(ss);
      let timeDown = {};
      if(ts > 0){
      timeDown.hh = hh;
      timeDown.mm = mm;
      timeDown.ss = ss;
      this.setData({
        timeDown
      })
      } else if(ts <= 0){
          clearTimeout(timer)
      }
      timer = setTimeout(() => {
        this.timeDown()
      }, 1000)
    },
    checkTime(i) {
      if (i < 10) {
        i = "0" + i;
      }
      return i;
    },
    openPage:function(e){
      var type = e.currentTarget.dataset.type;
      if (type == 1) {
        wx.showModal({
          title:'您确认要退诊查费吗？',
          content:'当前问诊费会在1-3个工作日原路返回，请耐心等待',
          showCancel:true,
          cancelText:'取消',
          confirmText:"确定",
          success: (res) => {
              if (res.confirm) {
                wx.showLoading({
                  title: "退款请求中",
                  mask: true
                });
                let params = {
                  "doctorId": this.data.feeData.doctorId,
                  "userId": JSON.parse($service.getStore('userInfo')).id,
                  "isFrozen": 0
                }
                app.http.refund(params).then(res => {
                  clearTimeout(timer)
                  $service.showToast('退费成功');
                  this.triggerEvent("refund")
                  this.setData({
                    isFee: false
                  })
                  this.getConsultationFee();
                }).catch(err => {
                  console.log(err)
                  $service.showToast(err.msg);
                }).finally(res => {
                  wx.hideLoading();
                })
              }
          } 
        }) 
      } else {
        if (this.data.feeData.isOnline === 1) {
          let {doctorId} = this.data.feeData
          app.http.getDoctorDetail({id: doctorId}).then(res => {
            let {inquiryType} = res.data
            if (inquiryType != '3') {
              if (inquiryType != this.data.feeData.inquiryType) {
                wx.showModal({
                  title: '提示',
                  showCancel: false,
                  content: `该医生暂不支持${this.data.feeData.inquiryType === '2' ? '图文问诊' : '视频问诊'}`,
                  success (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    }
                  }
                })
                return
              }
            }
            $service.openPage('/pages/user_side/inquiry_service/inquiry_write/inquiry_write?doctorId=' + doctorId + "&log_type=" + this.data.feeData.inquiryType)
          })
        } else {
          $service.showToast('医生不在线，请稍后重试');
        }
      }
    },
    closeBox:function(){
      this.setData({
        isFee:false
      })
    },
  }
})
